package com.ruoyi.dept.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.dept.domain.bo.BsLearnBo;
import com.ruoyi.dept.domain.vo.BsLearnVo;
import com.ruoyi.dept.domain.BsLearn;
import com.ruoyi.dept.mapper.BsLearnMapper;
import com.ruoyi.dept.service.IBsLearnService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 试验室培训Service业务层处理
 *
 * @author zyb
 * @date 2024-04-03
 */
@RequiredArgsConstructor
@Service
public class BsLearnServiceImpl implements IBsLearnService {

    private final BsLearnMapper baseMapper;

    /**
     * 查询试验室培训
     */
    @Override
    public BsLearnVo queryById(Long learnId){
        return baseMapper.selectVoById(learnId);
    }

    /**
     * 查询试验室培训列表
     */
    @Override
    public TableDataInfo<BsLearnVo> queryPageList(BsLearnBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BsLearn> lqw = buildQueryWrapper(bo);
        Page<BsLearnVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询试验室培训列表
     */
    @Override
    public List<BsLearnVo> queryList(BsLearnBo bo) {
        LambdaQueryWrapper<BsLearn> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BsLearn> buildQueryWrapper(BsLearnBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BsLearn> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getLearnId() != null, BsLearn::getLearnId, bo.getLearnId());
        lqw.eq(bo.getDeptId() != null, BsLearn::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getLearnType()), BsLearn::getLearnType, bo.getLearnType());
        lqw.eq(bo.getLearnDate() != null, BsLearn::getLearnDate, bo.getLearnDate());
        lqw.eq(StringUtils.isNotBlank(bo.getLearnAddress()), BsLearn::getLearnAddress, bo.getLearnAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getLearnStatus()), BsLearn::getLearnStatus, bo.getLearnStatus());
        return lqw;
    }

    /**
     * 新增试验室培训
     */
    @Override
    public Boolean insertByBo(BsLearnBo bo) {
        BsLearn add = BeanUtil.toBean(bo, BsLearn.class);
        add.setDeptId(LoginHelper.getDeptId());
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setLearnId(add.getLearnId());
        }
        return flag;
    }

    /**
     * 修改试验室培训
     */
    @Override
    public Boolean updateByBo(BsLearnBo bo) {
        BsLearn update = BeanUtil.toBean(bo, BsLearn.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BsLearn entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除试验室培训
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
